package dialog;

import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import model.Layer;
import utils.ImageOperationUtils;
import utils.ImageUtils;
import abstraction.ImageOperationDialog;

/**
 * 
 * @author Maxime
 */
public class PixelOperationsFilterDialog extends ImageOperationDialog implements ItemListener {

	private final float m_noConvolveMatrix[] = { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
			0.0f, 0.0f, 0.0f, 0.0f };

	private final float m_sharpenMatrix[] = { -1.0f, -1.0f, -1.0f, -1.0f, 9.0f,
			-1.0f, -1.0f, -1.0f, -1.0f };

	private final float m_blurMatrix[] = { 0.1111f, 0.1111f, 0.1111f, 0.1111f,
			0.1111f, 0.1111f, 0.1111f, 0.1111f, 0.1111f };

	/*private final float m_edgeDetectMatrix[] = { -0.5f, -0.5f, -0.5f, -0.5f,
			4.0f, -0.5f, -0.5f, -0.5f, -0.5f };*/

	private final float m_lightMatrix[] = { 0.1f, 0.1f, 0.1f, 0.1f, 1.0f, 0.1f,
			0.1f, 0.1f, 0.1f };

	private final float m_darkMatrix[] = { 0.01f, 0.01f, 0.01f, 0.01f, 0.5f,
			0.01f, 0.01f, 0.01f, 0.01f };

	private final float m_convolveMatrices[][] = { m_noConvolveMatrix,
			m_sharpenMatrix, m_blurMatrix, m_lightMatrix,
			m_darkMatrix };

	private final String convolveNames[] = { "Normal", "Sharpen", "Blur",
			 "Lighten", "Darken" };

	private float m_convolveMatrix[];
	private JComboBox m_convolveList;

	public PixelOperationsFilterDialog(JFrame parent, Layer layer) {
		super(parent, layer.getImage());
		m_titleLabel.setText("Pixel Operations");
		m_componentPanel.setLayout(new GridLayout(1, 1));
		m_convolveMatrix = m_noConvolveMatrix;
		m_convolveList = new JComboBox(convolveNames);
		m_componentPanel.add(m_convolveList);
		m_convolveList.addItemListener(this);

	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		m_convolveMatrix = m_convolveMatrices[m_convolveList.getSelectedIndex()];
		m_preview = ImageOperationUtils.createConvolveOp(m_current, m_convolveMatrix);
		m_imageLabel.setIcon(new ImageIcon(ImageUtils.resizeTo(m_preview,
				m_newWidth, m_newHeight)));
	}

}
